package com.geeksaga.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.geeksaga.sns.facebook.FacebookUtil;
import com.google.code.facebookapi.FacebookJsonRestClient;

@Controller
public class FaceBookController {
	
	private FacebookUtil facebookUtil;
	
	@RequestMapping( value = { "/sns/facebook/sharepost.do" }, method = RequestMethod.POST)
	public void sharePost(HttpServletRequest request, HttpServletResponse response) {
		
		String message = request.getParameter("message");
		
		System.out.println("sharepost.do");
		System.out.println(message);
		System.out.println("sharepost.do");
		
		FacebookJsonRestClient fJsonClient = facebookUtil.getFacebookJsonRestClient();
		facebookUtil.doStreamPublish(fJsonClient, message);
		
		try {
			response.getWriter().print(message);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	@RequestMapping( value = { "/sns/facebook/getcomment.do" }, method = RequestMethod.POST)
	public void getComment(HttpServletRequest request, HttpServletResponse response) {
		
		String postid = request.getParameter("postid");
		
		
		FacebookJsonRestClient fJsonClient = facebookUtil.getFacebookJsonRestClient();
		String html = facebookUtil.getComment(fJsonClient, postid);
		
		try {
			response.getWriter().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	@RequestMapping( value = { "/sns/facebook/facebookview.do" }, method = RequestMethod.GET)
	public String view(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		 
		facebookUtil = new FacebookUtil();
		
		String sessionKey = facebookUtil.getParamBySeeeionRequestValue(request.getParameter("session"), "session_key");
		facebookUtil.setSessionKey(sessionKey);
		
		FacebookJsonRestClient fJsonClient = facebookUtil.getFacebookJsonRestClient();
		
		JSONArray myInfo =  facebookUtil.getUserProfile(fJsonClient);
		JSONObject myInfoObject;
		
	    String myPicSrc = null;
	    String myName = null;
	    String myUid = null;
	    String myProfileLink = "http://www.facebook.com/profile.php?id=";
	    
		try {
			myInfoObject = myInfo.getJSONObject(0);
			myPicSrc = myInfoObject.getString("pic");
			myName = myInfoObject.getString("name");
			myUid = myInfoObject.getString("uid");
		} catch (JSONException e) {
			e.printStackTrace();
		}
	    
	    if(myPicSrc==null || myPicSrc == "null" || myPicSrc.equals(null) || myPicSrc.equals("null")){
	    	myPicSrc = "http://static.ak.fbcdn.net/rsrc.php/z5HB7/hash/ecyu2wwn.gif";
		}
	    
	    JSONArray friendJsonArray = facebookUtil.getUserFriendInformation(fJsonClient);
	    ArrayList<HashMap<String, String>> friendArray = facebookUtil.friendJsonArrayToArrayList(friendJsonArray);
	    
	    ArrayList<HashMap<String, Object>> recentFeedList = facebookUtil.getRecentFeedList(fJsonClient,20);
	    
	    
//	    System.out.println("commentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcomment");
//	    try {
//			System.out.println(fJsonClient.stream_getComments("100000841816093_128979303806796"));
//		} catch (FacebookException e) {
//			e.printStackTrace();
//		}
//	    System.out.println("commentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcommentcomment");
	    
	    
	    
	    modelMap.addAttribute("myFacebookLink", myPicSrc);
		modelMap.addAttribute("myPicSrc", myPicSrc);
		modelMap.addAttribute("myName", myName);
		modelMap.addAttribute("myUid", myUid);
		modelMap.addAttribute("myProfileLink", myProfileLink+myUid);
		modelMap.addAttribute("friendArray", friendArray);
		modelMap.addAttribute("recentMessage", recentFeedList);
		
		return "sns/facebook/facebookview";
	}
}
